package allen.log;

import java.util.Date;

import javax.swing.SwingUtilities;

/**
 * UI Logger. The log method of this class should be run in the ETD.
 * */
class UILogger implements Log {
	private String logName;

	public UILogger(String logName) {
		this.logName = logName;
	}

	@Override
	public void log(LogLevel level, Throwable t) {
		log(level, null, t);
	}

	@Override
	public void log(LogLevel level, String msg) {
		log(level, msg, null);

	}

	@Override
	public void log(LogLevel level, String msg, Throwable t) {

		if (msg == null && t != null) {
			msg = t.getMessage();
		}

		final LogEntity logEntity = new LogEntity(logName, new Date(), level,
				msg, t);

		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				LogDataCenter.logDataCenter.addLogEntity(logEntity);
			}
		});

	}

	@Override
	public void info(Throwable t) {
		info(null, t);
	}

	@Override
	public void info(String msg) {
		info(msg, null);
	}

	@Override
	public void info(String msg, Throwable t) {
		log(LogLevel.Info, msg, t);
	}

	@Override
	public void debug(Throwable t) {
		debug(null, t);

	}

	@Override
	public void debug(String msg) {
		debug(msg, null);

	}

	@Override
	public void debug(String msg, Throwable t) {
		log(LogLevel.Debug, msg, t);
	}

	@Override
	public void err(Throwable t) {
		err(null, t);

	}

	@Override
	public void err(String msg) {
		err(msg, null);
	}

	@Override
	public void err(String msg, Throwable t) {
		log(LogLevel.Error, msg, t);

	}

	@Override
	public void trace(Throwable t) {
		trace(null, t);

	}

	@Override
	public void trace(String msg) {
		trace(msg, null);

	}

	@Override
	public void trace(String msg, Throwable t) {
		log(LogLevel.Trace, msg, t);

	}

	@Override
	public void warn(Throwable t) {
		warn(null, t);
	}

	@Override
	public void warn(String msg) {
		warn(msg, null);
	}

	@Override
	public void warn(String msg, Throwable t) {
		log(LogLevel.Warn, msg, t);
	}

	@Override
	public void fatal(Throwable t) {
		fatal(null, t);

	}

	@Override
	public void fatal(String msg) {
		fatal(msg, null);
	}

	@Override
	public void fatal(String msg, Throwable t) {
		log(LogLevel.Fatal, msg, t);
	}

}
